package com.mobile.challenge.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.mobile.challenge.R;
import com.mobile.challenge.widget.SeatTable;

import java.util.ArrayList;
import java.util.List;

public class SeatChooseActivity extends AppCompatActivity {

    public SeatTable seatTableView;
    public static int mScreenWidth;
    private int maxRow = 11;
    private int maxColumn = 13;
    private int maxSeats = (maxRow + 1) * (maxColumn - 2);
    int halfColumn = (maxColumn - 1) / 2;
    private List<Integer> seatList = new ArrayList<>();

    private int rowNum = 0;
    private int columnNum = 0;
    private TextView tvSave;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_seat_choose);
        setScreenWidth();

        seatList.add(1);
        seatList.add(17);
        seatList.add(21);
        seatList.add(34);
        seatList.add(45);
        seatList.add(58);
        seatList.add(66);
        seatList.add(72);
        seatList.add(86);
        seatList.add(99);

        seatTableView = (SeatTable) findViewById(R.id.seatView);
        tvSave = (TextView) findViewById(R.id.tv_save);
        seatTableView.setScreenName("Door");//设置屏幕名称
        seatTableView.setMaxSelected(1);//设置最多选中

        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int seat = getSeatID(rowNum, columnNum);
                if (seat != 0) {
                    Intent intent = new Intent();
                    intent.putExtra("seatid", seat);
                    setResult(RESULT_OK,intent);
                    finish();
                }
            }
        });

        seatTableView.setSeatChecker(new SeatTable.SeatChecker() {

            @Override
            public boolean isValidSeat(int row, int column) {
                if (column == halfColumn) {
                    return false;
                }
                return true;
            }

            @Override
            public boolean isSold(int row, int column) {
                if (getSoldSeat(row, column)) {
                    return true;
                }
                return false;
            }

            @Override
            public void checked(int row, int column) {
                Log.e("----", "checked: " + row + " " + column);
                rowNum = row;
                columnNum = column;
            }

            @Override
            public void unCheck(int row, int column) {

            }

            @Override
            public String[] checkedSeatTxt(int row, int column) {
                return null;
            }

        });
        seatTableView.setData(11, 13);

        if (getIntent().getBooleanExtra("only_look",false)) {
            seatTableView.setMaxSelected(0);
            tvSave.setText("Back");
            seatList.add(25);
            seatList.add(37);
            seatList.add(43);
            seatList.add(55);
            seatList.add(69);
            seatList.add(74);
        }

    }

    private int getSeatID(int row, int column) {
        int correctNumber = 0;
        if (column < halfColumn) {
            correctNumber = halfColumn * (row + 1) - column;
        } else if (column > halfColumn) {
            correctNumber = row * halfColumn + column - halfColumn + maxSeats / 2;
        }
        return correctNumber;
    }


    private boolean getSoldSeat(int row, int column) {
        int correctNumber = 0;
        if (column == halfColumn) {
            return false;
        } else if (column < halfColumn) {
            correctNumber = halfColumn * (row + 1) - column;
        } else if (column > halfColumn) {
            correctNumber = row * halfColumn + column - halfColumn + maxSeats / 2;
        }
        return seatList.contains(correctNumber);
    }

    public void setScreenWidth() {
        WindowManager manager = this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;
    }
}
